﻿#include"stdafx.h"
#include"SQLexpression.h"
void SqlEx::Error()
{
	pSocket->SendMSG("Please choose a database !");
}


bool SqlEx::ForCreateUser()
{
	string db = CStringTostring(pSocket->cur_db);
	string user_host = "";
	string pwd = "",msg="";
	//create user username@host identified by passwordEND;
	regex re1("(?<=create user ).+?(?= identified by)");
	regex re2("(?<=identified by ).+?(?=END)");
	smatch sm;
	if (!authority->isRoot(this->user_host)) {
		pSocket->SendMSG("你没有创建用户的权限！！");
		return false;
	}

	if (regex_search(Ex, sm, re1)) {
			user_host = trimAll(sm.str(), " ");
	}
	else {
		pSocket->SendMSG("检查create user 和identified by语句之间是否存在错误！！！");
		return false;
	}
	if (regex_search(Ex, sm, re2)) {
		pwd = trimAll(sm.str()," ");
	}
	else {
		pSocket->SendMSG("检查identified by之后语句是否存在错误！！！");
		return false;
	}

	authority->User_insert(user_host,pwd,msg);
	pSocket->SendMSG(msg);
	return true;
}

bool SqlEx::ForShowUser()
{
	string msg = "";
	if (!authority->isRoot(this->user_host)) {
		pSocket->SendMSG("你没有权限访问user表！");
		return false;
	}
	authority->Show_Users(msg);
	pSocket->SendMSG(msg);
	return true;
}

bool SqlEx::ForGrant()
{
	//GRANT privileges ON databasename.tablename TO 'username'@'host'
	string user_host = "";
	string dbname = "", tname="",msg = "";
	string db = CStringTostring(pSocket->cur_db);

	regex re1("(?<=grant privileges on ).+?(?= to)");
	regex re2("(?<=to ).+?(?=END)");
	smatch sm;

	//默认root用户有权限授权
	if (!authority->isRoot(this->user_host)) {
		pSocket->SendMSG("你没有权限！！");
		return false;
	}

	if (regex_search(Ex, sm, re1)) {
		string temp = trimAll(sm.str(), " ");
		int pos = temp.find(".");
		if (pos != -1) {
			dbname = temp.substr(0, pos);
			tname = temp.substr(pos + 1, temp.length() - pos - 1);
		}
		else
			dbname = temp;
	}
	else {
		pSocket->SendMSG("检查grant privileges on 和to语句之间是否存在错误！！！");
		return false;
	}


	//判断数据库是否存在
	DB_Struct temp_db; 
	Conversion(temp_db.name, dbname.c_str());
	vector<DB_Struct>::iterator ret = find(serverDB->dbVector.begin(), serverDB-> dbVector.end(), temp_db);
	if (ret == serverDB->dbVector.end()) {
		pSocket->SendMSG("数据库不存在！");
		return false;
	}


	//判断表是否存在
	string  tpath; tpath = "data\\"+dbname+"\\"+dbname+".tb";
	if (tname!=""&&!IsTable(tpath,tname.c_str())) {
		pSocket->SendMSG("表名错误！");
		return false;
	}


	if (regex_search(Ex, sm, re2)) {
		user_host = trimAll(sm.str(), " ");
	}
	else {
		pSocket->SendMSG("检查to之后语句是否存在错误！！！");
		return false;
	}

	authority->Auth_insert(user_host, dbname, tname,msg);
	pSocket->SendMSG(msg);
	return false;
}


bool SqlEx::ForDropUser()
{
	string user_host = "",msg="";
	regex re1("(?<=drop user ).+?(?=END)");
	smatch sm;
	if (!authority->isRoot(this->user_host)) {
		pSocket->SendMSG("你没有删除用户的权限！！");
		return false;
	}

	if (regex_search(Ex, sm, re1)) {
		user_host = trimAll(sm.str(), " ");
	}
	else {
		pSocket->SendMSG("检查语句是否存在错误！！！");
		return false;
	}
	authority->User_Drop(user_host, msg);
	pSocket->SendMSG(msg);
	return true;
}


bool SqlEx::ForSetPwd()
{
	string user_host = "", msg = "",pwd="";
	regex re1("(?<=set password for ).+?(?=\\=)");
	regex re2("(?<=\\=).+?(?=END)");
	smatch sm;

	if (!authority->Auth_Judge(this->user_host, "system", "user")) {
		pSocket->SendMSG("You does not have permission to change pwd!");
		return false;
	}

	if (regex_search(Ex, sm, re1)) {
		user_host = trimAll(sm.str(), " ");
	}
	else {
		pSocket->SendMSG("检查语句是否存在错误！！！");
		return false;
	}
	if (regex_search(Ex, sm, re2)) {
		pwd = trimAll(sm.str(), " ");
	}
	else {
		pSocket->SendMSG("检查语句是否存在错误！！！");
		return false;
	}
	
	authority->Update_Pwd(user_host, pwd, msg);
	pSocket->SendMSG(msg);
	return false;
}


